package com.springboot.middleware.middleware_redisson.server.redisson;

import com.google.common.base.Strings;
import org.redisson.api.RQueue;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

@Component
public class QueueConsumer implements ApplicationRunner, Ordered {
    private static final Logger log = LoggerFactory.getLogger(QueueConsumer.class);

    @Autowired
    private RedissonClient redissonClient;

    /**
     * Callback used to run the bean.
     *
     * @param args incoming application arguments
     * @throws Exception on error
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        final String queueName = "redissonBasicQueue";
        RQueue<String> rQueue = redissonClient.getQueue(queueName);
        while (true) {
            String msg = rQueue.poll();
            if (!Strings.isNullOrEmpty(msg)) {
                log.info("队列的消费者 - 监听消费消息: {}", msg);
                // TODO: 2022/1/24 业务代码
            }
        }
    }

    /**
     * 表示QueueConsumer将会在项目启动之后启动
     *
     * @return -1
     */
    @Override
    public int getOrder() {
        return -1;
    }
}
